package com.renwei.yunlong.activity.news;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.CommonBean;
import com.renwei.yunlong.event.ChatNameRefreshEvent;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import io.rong.imkit.RongIM;
import io.rong.imkit.userInfoCache.RongUserInfoManager;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Group;
import io.rong.imlib.model.Message;
import io.rong.message.InformationNotificationMessage;

public class GroupNameSettingActivity extends BaseActivity {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.et_name)
    EditText etName;
    private String groupId;
    private String oldName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_group_name_setting);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        groupId = getIntent().getStringExtra("groupId");
        oldName = getIntent().getStringExtra("oldName");

        simpleTileView.setTitle("修改群名称");
        etName.setText(oldName);
        etName.setSelection(oldName.length());
        simpleTileView.setRightText("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String groupName = etName.getText().toString().trim();
                if (TextUtils.isEmpty(groupName)) {
                    showCenterInfoMsg("请输入群名称");
                } else {
                    updateName(groupName);
                }
            }
        });
    }

    private void updateName(String groupName) {
        HashMap<String, String> map = new HashMap<>();
        map.put("groupId", groupId);
        map.put("groupName", groupName);
        ServiceRequestManager.getManager().updateGroupName(this, JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                CommonBean commonBean = new Gson().fromJson(result, CommonBean.class);
                if (commonBean.getMessage().getCode() == 200) {
                    showCenterSuccessMsg("操作成功");
                    // 修改内存
                    RongUserInfoManager.getInstance().setGroupInfo(new Group(groupId, etName.getText().toString().trim(),  Uri.parse("xxx")));
                    // 发送通知消息
                    sendNotificationMsg();
                    // 聊天UI修改
                    EventBus.getDefault().post(new ChatNameRefreshEvent(etName.getText().toString().trim()));
                    finish();
                } else {
                    showCenterInfoMsg(commonBean.getMessage().getMessage());
                }
                break;
        }
    }

    private void sendNotificationMsg() {
        String name = "";
        if ("1".equals(companyType)) {
            name = ownerBean.getRows().getName();
        } else if ("2".equals(companyType)) {
            name = serviceLoginBean.getRows().getName();
        }
        InformationNotificationMessage message = InformationNotificationMessage.obtain(name + "修改群名称为"+etName.getText().toString().trim());
        Message myMessage = Message.obtain(groupId, Conversation.ConversationType.GROUP, message);
        RongIM.getInstance().sendMessage(myMessage, null, null, new IRongCallback.ISendMediaMessageCallback() {
            @Override
            public void onProgress(Message message, int i) {

            }

            @Override
            public void onCanceled(Message message) {

            }

            @Override
            public void onAttached(Message message) {

            }

            @Override
            public void onSuccess(Message message) {

            }

            @Override
            public void onError(Message message, RongIMClient.ErrorCode errorCode) {

            }
        });
    }

    public static void OpenActivity(Context context, String groupId, String oldName) {
        Intent intent = new Intent(context, GroupNameSettingActivity.class);
        intent.putExtra("groupId", groupId);
        intent.putExtra("oldName", oldName);
        context.startActivity(intent);
    }
}
